package org.jeecg.modules.demo.xiaocailiang.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.demo.xiaocailiang.entity.Xiaocailiang;
import org.jeecg.modules.demo.xiaocailiang.service.IXiaocailiangService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: xiaocailiang
 * @Author: jeecg-boot
 * @Date:   2021-11-22
 * @Version: V1.0
 */
@Api(tags="xiaocailiang")
@RestController
@RequestMapping("/jxc/xiaocailiang")
@Slf4j
public class XiaocailiangController extends JeecgController<Xiaocailiang, IXiaocailiangService> {
	@Autowired
	private IXiaocailiangService xiaocailiangService;
	
	/**
	 * 分页列表查询
	 *
	 * @param xiaocailiang
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "xiaocailiang-分页列表查询")
	@ApiOperation(value="xiaocailiang-分页列表查询", notes="xiaocailiang-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(Xiaocailiang xiaocailiang,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<Xiaocailiang> queryWrapper = QueryGenerator.initQueryWrapper(xiaocailiang, req.getParameterMap());
		Page<Xiaocailiang> page = new Page<Xiaocailiang>(pageNo, pageSize);
		IPage<Xiaocailiang> pageList = xiaocailiangService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param xiaocailiang
	 * @return
	 */
	@AutoLog(value = "xiaocailiang-添加")
	@ApiOperation(value="xiaocailiang-添加", notes="xiaocailiang-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody Xiaocailiang xiaocailiang) {
		xiaocailiangService.save(xiaocailiang);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param xiaocailiang
	 * @return
	 */
	@AutoLog(value = "xiaocailiang-编辑")
	@ApiOperation(value="xiaocailiang-编辑", notes="xiaocailiang-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody Xiaocailiang xiaocailiang) {
		xiaocailiangService.updateById(xiaocailiang);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "xiaocailiang-通过id删除")
	@ApiOperation(value="xiaocailiang-通过id删除", notes="xiaocailiang-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		xiaocailiangService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "xiaocailiang-批量删除")
	@ApiOperation(value="xiaocailiang-批量删除", notes="xiaocailiang-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.xiaocailiangService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "xiaocailiang-通过id查询")
	@ApiOperation(value="xiaocailiang-通过id查询", notes="xiaocailiang-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		Xiaocailiang xiaocailiang = xiaocailiangService.getById(id);
		if(xiaocailiang==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(xiaocailiang);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param xiaocailiang
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, Xiaocailiang xiaocailiang) {
        return super.exportXls(request, xiaocailiang, Xiaocailiang.class, "xiaocailiang");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, Xiaocailiang.class);
    }


    //首页下方按月统计销售额
	 @GetMapping(value = "/getListBymonth")
	 public Result<?> getListBymonth(Xiaocailiang xiaocailiang,
									@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									HttpServletRequest req) {
		 List<Xiaocailiang> pageList = xiaocailiangService.getListBymonth(xiaocailiang);
		 return Result.OK(pageList);
	 }

	 //首页按月统计销售额
	 @GetMapping(value = "/getShouyethree")
	 public Result<?> getShouyethree() {
    	Map<String,Object> map = new HashMap<>();
		 Xiaocailiang list = xiaocailiangService.getSumxl();
		 Xiaocailiang orderlist = xiaocailiangService.getOrdersl();
		 Xiaocailiang paydata = xiaocailiangService.getPaydata();
		 map.put("getSumxl1",list);
		 map.put("getOrdersl",orderlist);
		 map.put("getpaydata",paydata);
		 return Result.OK(map);
	 }

	 //首页收入盈亏平衡点分析
	 @GetMapping(value = "/getShourufenxi")
	 public Result<?> getShourufenxi() {
		 Xiaocailiang list = xiaocailiangService.getShourufenxi();
		 return Result.OK(list);
	 }

	 //首页echarts图表
	 @GetMapping(value = "/getEChartsdata")
	 public Result<?> getEChartsdata() {
		 List<Xiaocailiang> list = xiaocailiangService.getEChartsdata();
		 return Result.OK(list);
	 }
}
